import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel';

import { ErrorMessageMode } from '/#/axios';

enum Api {
  Login = '/v1/apisrv/login',
  Logout = '/logout',
  GetUserInfo = '/getUserInfo',
  GetPermCode = '/getPermCode',
  GetUserList = '/v1/apisrv/users',
  GetRoleList = '/v1/apisrv/roles',
  GetFunctionCodes = '/api/v1/apisrv/functions',
  DeviceList = '/v1/apisrv/asset',
  DeviceAll = '/v1/apisrv/asset/list',
  GetPatrolUser = '/v1/apisrv/patrol/users',

  PatrolCount = '/v1/apisrv/patrol/info/count',
  Label = `/v1/apisrv/label`,
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export interface UserItem {
  id: number;
  account: string;
  name: string;
  type: number;
  phone: string;
  email: string;
  pid: number;
  avatar: string;
  desc: string;
  last_login_time: number;
  create_time: number;
  update_time: number;
  enable: number;
  state: number;
}

export interface UserPagion {
  page: number;
  page_size: number;
  total: number;
  rows: UserItem[];
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout });
}

export const getUserList = (params) =>
  defHttp.get<UserPagion>({ url: Api.GetUserList, params }, { errorMessageMode: 'none' });

export const addUser = (params: UserItem) =>
  defHttp.post({ url: Api.GetUserList, params }, { errorMessageMode: 'none' });

export const editUser = (id, params) =>
  defHttp.put({ url: Api.GetUserList + '/' + id, params }, { errorMessageMode: 'none' });
export const editPassword = (id, params: { password: string; new_password: string }) =>
  defHttp.post({ url: Api.GetUserList + '/pwd/' + id, params }, { errorMessageMode: 'none' });
export const deleteUser = (id) =>
  defHttp.delete({ url: Api.GetUserList + '/' + id }, { errorMessageMode: 'none' });

// 巡护用户
export const getPatrolUserList = (params) =>
  defHttp.get<UserPagion>({ url: Api.GetPatrolUser, params }, { errorMessageMode: 'none' });

export const addPatrolUser = (params: UserItem) =>
  defHttp.post({ url: Api.GetPatrolUser, params }, { errorMessageMode: 'none' });

export const editPatrolUser = (id, params) =>
  defHttp.put({ url: Api.GetPatrolUser + '/' + id, params }, { errorMessageMode: 'none' });
export const editPatrolPassword = (id, params: { password: string; new_password: string }) =>
  defHttp.post({ url: Api.GetPatrolUser + '/pwd/' + id, params }, { errorMessageMode: 'none' });
export const deletePatrolUser = (id) =>
  defHttp.delete({ url: Api.GetPatrolUser + '/' + id }, { errorMessageMode: 'none' });

// 角色管理

export interface RoleItem {
  id: number;
  name: string;
  create_time: number;
  update_time: number;
  enable: number;
  state: number;
}

export interface CodeItem {
  id: number;
  name: string;
  code: string;
  type: number;
  p_id: number;
}

export const getFunctionCodes = () =>
  defHttp.get<CodeItem[]>({ url: Api.GetFunctionCodes }, { errorMessageMode: 'none' });
export const bindRoleCodes = (params: { role_id: number; function_codes: string[] }) =>
  defHttp.post({ url: '/v1/apisrv/roles/functions', params }, { errorMessageMode: 'none' });
// 1:电话号码 2：电话分组
export const bindRoleCameras = (params: { role_id: number; asset_nums: any[] }) =>
  defHttp.post({ url: '/v1/apisrv/roles/camera', params }, { errorMessageMode: 'none' });
export const bindUserRoles = (params: { user_id: number; role_ids: number[] }) =>
  defHttp.post({ url: '/v1/apisrv/userroles', params }, { errorMessageMode: 'none' });

export const getRoleList = () =>
  defHttp.get<RoleItem[]>({ url: Api.GetRoleList }, { errorMessageMode: 'none' });
export const addRole = (params: { name: string }) =>
  defHttp.post({ url: '/v1/apisrv/roles', params }, { errorMessageMode: 'none' });

export const editRole = (id, params: { name: string; enable: number }) =>
  defHttp.put({ url: `/v1/apisrv/roles/${id}`, params }, { errorMessageMode: 'none' });

export const deleteRole = (id) =>
  defHttp.delete({ url: `/v1/apisrv/roles/${id}` }, { errorMessageMode: 'none' });

// 分组管理

export interface GroupItem {
  id: number;
  name: string;
  prefix: string;
  suffix: string;
}
export const getGroupList = () =>
  defHttp.get<GroupItem[]>({ url: '/v1/apisrv/asset/group' }, { errorMessageMode: 'none' });
export const addGroup = (params: GroupItem) =>
  defHttp.post({ url: '/v1/apisrv/asset/group', params }, { errorMessageMode: 'none' });

export const editGroup = (id, params: GroupItem) =>
  defHttp.put({ url: `/v1/apisrv/asset/group/${id}`, params }, { errorMessageMode: 'none' });

export const deleteGroup = (id) =>
  defHttp.delete({ url: `/v1/apisrv/asset/group/${id}` }, { errorMessageMode: 'none' });

export const getUserRole = (user_id) =>
  defHttp.get<RoleItem[]>(
    { url: '/v1/apisrv/userroles', params: { user_id } },
    { errorMessageMode: 'none' },
  );

export interface GetDeviceParams {
  status: string;
  type: string;
  param_search: string;
  page: string;
  page_size: string;
}

export interface DeviceItem {
  id: number;
  mac_code: string;
  sim_number: string;
  type: number;
  model: string;
  buy_time: number;
  status: number;
  longitude: string;
  latitude: string;
  insert_time: number;
  scrap_time: number;
  remark: string;
}

export function getDeviceList(params?: GetDeviceParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<{ rows: DeviceItem[] }>(
    {
      url: Api.DeviceList,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export function getDeviceAll(params?: GetDeviceParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<DeviceItem[]>(
    {
      url: Api.DeviceAll,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

export interface RoleBindCameraItem {
  id: number;
  role_id: number;
  type: number;
  number: string;
}

export function getRoleBindCameras(role_id) {
  return defHttp.get<RoleBindCameraItem[]>({
    url: '/v1/apisrv/roles/camera',
    params: { role_id },
  });
}

export function getPatrolRecord(params: {
  start_time: number;
  end_time: number;
  param_search: string;
}) {
  return defHttp.get<any>({
    url: '/v1/apisrv/patrol/info',
    params: params,
  });
}

export function getFunCodes() {
  return defHttp.get<any>({
    url: '/v1/apisrv/functions',
  });
}
export function getRoleFunCodes(role_id: string | number) {
  return defHttp.get<any>({
    url: '/v1/apisrv/roles/function/' + role_id,
  });
}

export function editRoleFunCodes(role_id: string | number, function_codes: string[]) {
  return defHttp.post<any>({
    url: '/v1/apisrv/roles/functions',
    params: {
      role_id,
      function_codes,
    },
  });
}

export function handlePatrolItem(id, handler: string, desc: string) {
  return defHttp.put({
    url: `/v1/apisrv/patrol/info/${id}`,
    params: {
      handler,
      handler_descr: desc,
    },
  });
}

export function batchHandlePatrol() {
  return defHttp.put({
    url: `/v1/apisrv/patrol/info`,
  });
}

export function getPatrolCount(params: { start_time: number; end_time: number; type: number }) {
  return defHttp.get<RoleBindCameraItem[]>({
    url: Api.PatrolCount,
    params: params,
  });
}

export interface EditPasswordParams {
  password: string;
  new_password: string;
}

export function getLabelList() {
  return defHttp.get<LabelItem[]>({
    url: Api.Label,
  });
}

export function addLabel(params: LabelItem) {
  return defHttp.post<any>({
    url: Api.Label,
    params,
  });
}

export function editLabel(id: number, params) {
  return defHttp.put<LabelItem[]>({
    url: `${Api.Label}/${id}`,
    params,
  });
}

export function deleteLabel(id) {
  return defHttp.delete<LabelItem[]>({
    url: `${Api.Label}/${id}`,
  });
}

// export function editPassword(params: EditPasswordParams) {
//   return defHttp.post({
//     url: '/api/v1/apisrv/users/pwd/{id}',
//     params,
//   });
// }

export interface LabelItem {
  id: number;
  name: string;
}
